package com.alibaba.cirtus.ots.application.adapter.ability.facade.service.crm;

import com.alibaba.acm.shaded.com.google.common.collect.Maps;
import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.cirtus.ots.application.adapter.ability.model.response.CallApiRecordResponse;
import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmCommResponse;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmCommSDO;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmIf005Request;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmIf005SDO;
import com.epoch.app.otsapplicationadaptersdk.crm.mncrmif005.service.MnCrmIf005Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MessageHeaderPassiveSDO;
import com.epoch.app.otspricecenter.model.dto.SyncActivityTitleExtDataRequest;
import com.epoch.app.otspricecenter.model.dto.SyncActivityTitleExtDataResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.Objects;

/**
 * @author hanguo
 * @date 2021/10/12
 * @Description 促销活动更改接口（活动量及活动金额）
 */
@Component
public class MnCrmIf005ServiceImpl implements MnCrmIf005Service {

    private static Log log = Log.getLogger(MnCrmIf005ServiceImpl.class);

    private static final String MESSAGE_ID = "messageId";

    private static final String DATA_SOURCE = "dataSource";

    @Override
    public SyncActivityTitleExtDataResponse externalSyncUpdatePromotionActivity$syncActivityTitleExtData(SyncActivityTitleExtDataRequest input) {
        return MnCrmIf005Service.super.externalSyncUpdatePromotionActivity$syncActivityTitleExtData(input);
    }

    @ProcessInvoker(remark = "MnCrmIf005Service", errorCode = "", printLog = true)
    @Override
    public MnCrmCommResponse externalSyncUpdatePromotionActivity(MnCrmIf005Request mnCrmIf005Request) {
        MnCrmCommResponse mnCrmCommResponse = new MnCrmCommResponse();
        mnCrmCommResponse.setMessageId(mnCrmIf005Request.getMessageHeader().getMessageId());
        mnCrmCommResponse.setInterfacePath(mnCrmIf005Request.getMessageHeader().getInterfacePath());
        MnCrmCommSDO mnCrmCommSDO = new MnCrmCommSDO();

        //数据来源记录
        MessageHeaderPassiveSDO messageHeader = mnCrmIf005Request.getMessageHeader();
        Map<String,String> features = Maps.newHashMap();
        if(Objects.nonNull(messageHeader)){
            //数据头信息id
            features.put(MESSAGE_ID,messageHeader.getMessageId());
            //数据来源 sap或者excel等
            features.put(DATA_SOURCE, messageHeader.getSender());
        }

        CallApiRecordResponse callApiRecordResponse = new CallApiRecordResponse();

        if (Objects.nonNull(mnCrmIf005Request.getData())) {
            MnCrmIf005SDO data = mnCrmIf005Request.getData();
            SyncActivityTitleExtDataRequest syncActivityTitleExtDataRequest = new SyncActivityTitleExtDataRequest();
            syncActivityTitleExtDataRequest.setActivityNoteId(data.getActivityNoteId());
            syncActivityTitleExtDataRequest.setApplyItemQuantity(SapAndCrmFieldDealUtils.toLong(data.getApplyItemQuantity()));
            String applyFee = data.getApplyFee();
            if (StringUtils.isNotBlank(applyFee)) {
                BigDecimal multiply = new BigDecimal(applyFee.trim()).multiply(new BigDecimal(100));
                syncActivityTitleExtDataRequest.setApplyFee(multiply.setScale(2, RoundingMode.HALF_DOWN).longValue());
            }
            //syncActivityTitleExtDataRequest.setApplyFee(SapAndCrmFieldDealUtils.toLong(data.getApplyFee()));
            syncActivityTitleExtDataRequest.setSalesUnit(data.getSalesUnit());
            syncActivityTitleExtDataRequest.setCurrency(data.getCurrency());
            syncActivityTitleExtDataRequest.setFeatures(JSONObject.toJSONString(features));

            SyncActivityTitleExtDataResponse syncActivityTitleExtDataResponse = this.externalSyncUpdatePromotionActivity$syncActivityTitleExtData(syncActivityTitleExtDataRequest);
            log.info("促销活动修改金额和数量 result = {}", JSONObject.toJSONString(syncActivityTitleExtDataResponse));
            if (!syncActivityTitleExtDataResponse.getSuccess()) {
                mnCrmCommSDO.setResultType(MnSAPStatus.ERROR.getCode());
                mnCrmCommSDO.setResultMessage(syncActivityTitleExtDataResponse.getMessage());
            }
            callApiRecordResponse.setMessage(syncActivityTitleExtDataResponse.getMessage());
            callApiRecordResponse.setOperateResult(callApiRecordResponse.getOperateResult());
            callApiRecordResponse.setTableName("ofs_activity_title_ext");
            callApiRecordResponse.setRequestBody(JSONObject.toJSONString(syncActivityTitleExtDataRequest));
        }else{
            mnCrmCommSDO.setResultType(MnSAPStatus.ERROR.getCode());
            mnCrmCommSDO.setResultMessage("请传数据!");
        }
        //如果没有执行出错的记录
        if (StringUtils.isBlank(mnCrmCommSDO.getResultType())&&StringUtils.isBlank(mnCrmCommSDO.getResultMessage())) {
            mnCrmCommSDO.setResultType(MnSAPStatus.SUCCESS.getCode());
            mnCrmCommSDO.setResultMessage("同步成功");
        }

        log.info("MnCrmIf005ServiceImpl_externalSyncUpdatePromotionActivity => 执行结束修改结果 = {}",JSONObject.toJSONString(callApiRecordResponse));

        mnCrmCommResponse.setData(mnCrmCommSDO);

        return mnCrmCommResponse;
    }
}
